package com.pdh.controller;

import com.pdh.bean.Message;
import com.pdh.bean.User;
import com.pdh.mvc.ResponseBody;
import com.pdh.service.UserService;
import com.pdh.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 彭德华
 * @Date 2021-08-29 22:19
 */
public class UserController {
    @ResponseBody("/user/consoleUser.do")
    public String consoleUser(HttpServletRequest req,HttpServletResponse resp) {
        //有一个固定的消息对象
        Message message = UserService.consoleUser();
        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        //0.获取是否分页标志
        boolean paging = Boolean.parseBoolean(req.getParameter("paging"));
        //1.获取查询数据的起始索引值(偏移量)
        int offset = Integer.parseInt(req.getParameter("offset"));
        //2.获取当前页要查询的数量，注意不是当前页码 计算机不需要页码
        int limit = Integer.parseInt(req.getParameter("limit"));
        //3.查询数据
        Message msg = UserService.findAll(paging,offset,limit);
        /**
         *  String json = JSONUtil.toJSON(msg.getData()); 错误，不能被前端直接识别,BootStrapTable的处理风格
         *  它必须返回的ResultData的数据格式才能被前端成功接收到
         */
        String json = JSONUtil.toJSON(msg.getData());
        return json;
    }

    @ResponseBody("/user/findByUserPhone.do")
    public String findByUserPhone(HttpServletRequest req,HttpServletResponse resp) {
        //1.接收参数
        String userPhone = req.getParameter("userPhone");
        //2.调用方法查询对应的用户
        Message message = UserService.findByUserPhone(userPhone);
        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        //1.接收四个参数
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        String userIdCardNo = req.getParameter("userIdCardNo");
        String password = req.getParameter("password");
        //2传递收到的四个数据
        User user = new User(username, userPhone, password, userIdCardNo);
        Message message = UserService.insert(user);
        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest req,HttpServletResponse resp){
        //1.获取参数
        int id = Integer.parseInt(req.getParameter("id"));
        //2.执行删除操作
        Message message = UserService.delete(id);
        String json = JSONUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest req,HttpServletResponse resp){
        //1.接收参数
        int id = Integer.parseInt(req.getParameter("id"));
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        String userIdCardNo = req.getParameter("userIdCardNo");
        String password = req.getParameter("password");
        //2.创建新User对象
        User newUser = new User(username, userPhone, password, userIdCardNo);
        //3.执行更新操作
        Message message = UserService.update(id, newUser);
        String json = JSONUtil.toJSON(message);
        return json;
    }
}
